#ifndef _CONN_NODE_CLIENT_H__
#define _CONN_NODE_CLIENT_H__

#include "conn_node.h"
#include "shm_ipc.h"
#include "game_event.h"
#include "websocket.h"

//#define MAX_CLIENT_SEND_BUFFER_SIZE (1024*256)

class conn_node_client: public conn_node_base
{
public:
	conn_node_client();
	virtual ~conn_node_client();

	virtual int get_listen_fd();
	virtual int recv_func(int fd);

	static conn_node_base *get_conn_node(int fd);
	int del_node();
	int clean_node();
	
	static std::map<int, conn_node_client *> map_fd_nodes;
	static std::map<uint64_t, conn_node_client *> map_player_id_nodes;
	static std::map<uint32_t, conn_node_client *> map_open_id_nodes;	

	static int add_map_fd_nodes(conn_node_client *client);
	static int add_map_player_id_nodes(conn_node_client *client);
	static int add_map_open_id_nodes(conn_node_client *client);		
	static conn_node_client * get_nodes_by_fd(int fd, uint16_t port);
	static conn_node_client * get_nodes_by_player_id(uint64_t player_id);
	static conn_node_client * get_nodes_by_open_id(uint32_t open_id);	

	void send_data_to_client();
	void send_login_other_device();

public:
	uint16_t login_seq;  //登录的seq号，登录包返回的时候比较这个seq，不一致则丢弃
	uint16_t seq;       //客户端发包的seq号，每次加1
	uint32_t open_id;
	uint64_t player_id;

	static int listen_fd;

		//websocket
	bool handshake;
	uint8_t conn_step;
	frame_t frame;

private:
	void memmove_data();
	void remove_buflen(int len);
	int respond_websocket_request();
	int recv_from_fd();
	int recv_handshake(int fd);
	int frame_read_cb(int fd);
	void on_recv_frame();

	int decode_and_check_crc(PROTO_HEAD *head);
	int dispatch_message();
	int send_player_exit(bool again = false);
	int send_hello_resp();	
};

extern shm_ipc_obj *ipc_game_wr;
extern shm_ipc_obj *ipc_login_wr;
extern shm_ipc_obj *ipc_fbpay_wr;
#endif
